#
# Catz - the world's most advanced cat show photo engine
# Copyright (c) 2010-2011 Heikki Siltala
# Licensed under The MIT License
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

use 5.12.0;
use strict;
use warnings;

# unbuffered outputs
# from http://perldoc.perl.org/functions/open.html
select STDERR;
$| = 1;
select STDOUT;
$| = 1;

use Test::More;
use Test::Mojo;

use Catz::Data::Conf;
use Catz::Data::Text;
use Catz::Data::List;

my $t = Test::Mojo->new ( conf ( 'app' ) );

my $matrix = list_matrix;

my @oksetups = qw ( en fi en211211 fi171212 en394211 fi211111 );

my $setup;
my $txt;

# a few list/mode combinations
my @oklists = qw (
 album/cron date/cron loc/first org/top umb/a2z cat/a2z cat/top
 cat/first breeder/top nat/first code/a2z app/top breed/cate cate/a2z
 feat/first nick/top title/first lens/a2z body/top fnum/first
 etime/a2z iso/top flen/a2z
);

# non-working combinations
my @badlists = qw (
 date/first umb/cron etime/cate folder/first nonexistent/a2z
);

# non-working paths
my @badpaths = qw ( /list/ /list/a2z/ /list/cat/ /list/cat/a2z/ );

# test list index

foreach $setup ( @oksetups ) {

 $txt = text ( substr ( $setup, 0, 2 ) );

 $t->get_ok ( "/$setup/lists/" )->status_is ( 200 )
  ->content_type_like ( qr/text\/html/ )
  ->content_like ( qr/$txt->{LISTINGS_LONG}/ );

 $t->get_ok ( "/$setup/lists" )->status_is ( 301 );

}

foreach my $listi ( @oklists ) {

 $setup = @oksetups[ rand @oksetups ];
 $txt = text ( substr ( $setup, 0, 2 ) );

 $listi =~ m|^(.+)\/(.+)$|;

 my $list = $1;
 my $mode = $2;

 if ( $matrix->{ $list }->{ dividers } ) {

  $t->get_ok ( "/$setup/list/$list/$mode/" )->status_is ( 200 )
   ->content_type_like ( qr/text\/html/ )
   ->content_like      ( qr/$txt->{'MODE_'.uc($mode)}/ )
   ->content_like      ( qr/\".{0,15}outer.{0,15}\"/ );

 }
 else {

  $t->get_ok ( "/$setup/list/$list/$mode/" )->status_is ( 200 )
   ->content_type_like ( qr/text\/html/ )
   ->content_like      ( qr/$txt->{'MODE_'.uc($mode)}/ )
   ->content_like      ( qr/\".{0,15}inner.{0,15}\"/ );

 }

 # no ending slash -> 301
 $t->get_ok ( "/$setup/list/$list/$mode" )->status_is ( 301 );

 # no mode -> 404
 $t->get_ok ( "/$setup/list/$list/" )->status_is ( 404 );

} ## end foreach my $listi ( @oklists)

foreach my $listi ( @badlists ) {

 $setup = @oksetups[ rand @oksetups ];
 $txt = text ( substr ( $setup, 0, 2 ) );

 $t->get_ok ( "/$setup/list/$listi/" )->status_is ( 404 );

}

foreach my $path ( @badpaths ) { $t->get_ok ( $path )->status_is ( 404 ) }

done_testing;
